<template>
  <div class="purchase-detail-wrap">
    <div class="header flex-c-sb">
      <a-space>
        <p>采购计划详情</p>
        <span style="font-weight: bold">{{ purchaseDetail.billNo }}</span>
        <a-tag color="arcoblue" size="small" bordered>{{ statusMap[purchaseDetail.status] }}</a-tag>
      </a-space>
      <div class="flex-c-c handle-btn-w">
        <a-space>
          <a-button class="gray-outline" size="medium" @click="closePage">关闭</a-button>
          <a-button v-if="purchaseDetail.status == 1 && $authPass('procurement.purchase.plan.edit')" type="primary" size="medium" @click="showEditDialog = true">编辑</a-button>
          <a-button v-if="purchaseDetail.status == 1 && $authPass('procurement.purchase.plan.submit')" type="primary" size="medium" @click="submitAudit">提交审核</a-button>
          <a-button v-if="purchaseDetail.status == 10 && $authPass('procurement.purchase.plan.audit')" type="primary" size="medium" @click="showAuditDialog = true">审核</a-button>
          <a-button v-if="purchaseDetail.status == 1 && $authPass('procurement.purchase.plan.cancel')" class="gray-outline" size="medium" @click="submitCancellation">作废</a-button>
          <a-button v-if="(purchaseDetail.status == 20 || purchaseDetail.status == 10) && $authPass('procurement.purchase.plan.revoke')" class="gray-outline" size="medium" @click="submitRevocation">撤回草稿</a-button>
        </a-space>
      </div>
    </div>
    <div class="base-w">
      <div class="base-con">
        <a-steps :current="currentStep">
          <a-step>
            <template #description>
              <p>{{ $setTime(purchaseDetail.gmtCreate) }}</p>
            </template>
            添加采购计划
          </a-step>
          <a-step>
            <template #description>
              <p v-if="currentStep > 2 && purchaseDetail.gmtAudit">{{ $setTime(purchaseDetail.gmtAudit) }}</p>
            </template>
            审核采购计划
          </a-step>
          <a-step>
            <template #description>
              <p v-if="purchaseDetail.gmtFirstPurchase">{{ $setTime(purchaseDetail.gmtFirstPurchase) }}</p>
            </template>
            采购中
          </a-step>
          <a-step>
            <template #description v-if="purchaseDetail.gmtFinished">
              <p>{{ $setTime(purchaseDetail.gmtFinished) }}</p>
            </template>
            完成
          </a-step>
        </a-steps>
        <a-row style="margin-top: 30px">
          <a-col :span="24">
            <p>
              <span class="label">目的仓库：</span>
              <span>{{ purchaseDetail.warehouseName }}({{ purchaseDetail.warehouseCode }})</span>
            </p>
          </a-col>
        </a-row>
        <a-row style="margin-top: 20px">
          <a-col :span="10">
            <div class="flex">
              <span class="label" style="flex-shrink: 0">附件：</span>
              <ul style="margin-right: 10px" v-if="purchaseDetail.attachment">
                <li v-for="(url, i) in purchaseDetail.attachment.split(',')" style="margin-bottom: 4px" :key="i">
                  <a-tag color="arcoblue" class="cursor" @click="downloadFile(url)">
                    <span>{{ url }}</span>
                    <icon-to-bottom />
                  </a-tag>
                </li>
              </ul>
              <p v-else>--</p>
            </div>
          </a-col>
          <a-col :span="14">
            <div class="flex">
              <span class="label">备注：</span>
              <span style="flex: 1">{{ purchaseDetail.remark }}</span>
            </div>
          </a-col>
        </a-row>
      </div>
    </div>
    <div class="purchase-detail-con">
      <a-tabs v-model:active-key="activeTab">
        <a-tab-pane key="1" title="采购计划明细" />
        <a-tab-pane key="2" title="关联采购单" />
        <!-- <a-tab-pane key="3" title="关联入库单" /> -->
        <a-tab-pane key="4" title="操作日志" />
      </a-tabs>
      <div class="purchase-detail-order">
        <PurchasePlanItem v-if="activeTab == '1'" :currentData="purchaseDetail" />
        <PurchasePlanOrderItem v-if="activeTab == '2'" :currentData="purchaseDetail" />
        <PurchaseLogs v-if="activeTab == '4'" :currentData="purchaseDetail" />
      </div>
    </div>
  </div>
  <!-- 编辑采购计划 -->
  <EditPurchsePlan v-if="showEditDialog" :isShow="showEditDialog" :currentData="purchaseDetail" @closed="showEditDialog = false" @success="getPurchaseDetail" />
  <!-- 审核 -->
  <Audit v-if="showAuditDialog" :isShow="showAuditDialog" @closed="showAuditDialog = false" @success="audit" />
  <!--  -->
</template>
<script lang="ts" setup>
  import { ref, onMounted } from 'vue'
  import { Message, Modal } from '@arco-design/web-vue'
  import useVisitedRouteStore from '@/store/modules/visited-routes'
  import { useRouter, useRoute } from 'vue-router'
  import EditPurchsePlan from './components/editPurchsePlan.vue'
  import Audit from './components/audit.vue'
  import PurchasePlanItem from './components/purchasePlanItem.vue'
  import PurchasePlanOrderItem from './components/purchasePlanOrderItem.vue'
  import { getPurchasePlanInfo, commitPurchasePlan, revokePurchasePlan, auditPurchasePlan, cancelPurchasePlan } from '@/api/purchase'
  import PurchaseLogs from './components/purchaseLogs.vue'

  onMounted(() => {
    getPurchaseDetail()
  })

  const statusMap = {
    0: '已废弃',
    1: '草稿中',
    10: '待审核',
    20: '待采购',
    30: '采购中',
    40: '已完成',
  }
  const visitedRouteStore = useVisitedRouteStore()
  const route = useRoute()
  const router = useRouter()
  const showEditDialog = ref(false)
  const showAuditDialog = ref(false)
  const purchaseDetail = ref<any>({})
  const currentStep = ref(1)
  const activeTab = ref('1')

  // 获取采购计划详情
  async function getPurchaseDetail() {
    const res: any = await getPurchasePlanInfo({ id: route.query.id })
    purchaseDetail.value = res?.data || {}
    let status = purchaseDetail.value.status
    if (status == 1) {
      currentStep.value = 1
    } else if (status == 10) {
      currentStep.value = 2
    } else if (status == 20 || status == 30) {
      currentStep.value = 3
    } else {
      currentStep.value = 4
    }
  }

  //  下载
  function downloadFile(url: string) {
    let _url = url
    if (_url.indexOf('?') > -1) {
      _url += '&response-content-type=application/octet-stream'
    } else {
      _url += '?response-content-type=application/octet-stream'
    }
    let link = document.createElement('a')
    link.href = _url
    link.click()
  }

  // 关闭界面
  function closePage() {
    visitedRouteStore.removeVisitedRoute(route)
    router.push('/purchase/purchaseManagement/purchasePlan')
  }
  // 提交审核
  function submitAudit() {
    Modal.confirm({
      title: '提交审核',
      content: `请再次确认是否需要提交审核`,
      cancelText: '取消',
      okText: '确认',
      titleAlign: 'start',
      onOk: async () => {
        const res: any = await commitPurchasePlan({ ids: [purchaseDetail.value.id] })
        if (res) {
          Message.success('提交审核成功')
          getPurchaseDetail()
        }
      },
    })
  }
  // 开始审核
  async function audit(auditRes: any) {
    const res: any = await auditPurchasePlan({
      ids: [purchaseDetail.value.id],
      pass: auditRes.state,
      remark: auditRes.result,
    })
    if (res) {
      Message.success(auditRes.state ? '审核通过' : '已驳回')
      getPurchaseDetail()
    }
  }
  // 作废
  function submitCancellation() {
    Modal.confirm({
      title: '作废',
      content: `请再次确认是否作废该采购计划`,
      cancelText: '取消',
      okText: '确认',
      titleAlign: 'start',
      onOk: async () => {
        const res: any = await cancelPurchasePlan({ ids: [purchaseDetail.value.id] })
        if (res) {
          Message.success('作废成功')
          getPurchaseDetail()
        }
      },
    })
  }
  // 撤回草稿
  function submitRevocation() {
    Modal.confirm({
      title: '撤回草稿',
      content: `请再次确认是否把该采购计划撤回到草稿箱`,
      cancelText: '取消',
      okText: '确认',
      titleAlign: 'start',
      onOk: async () => {
        const res: any = await revokePurchasePlan({ ids: [purchaseDetail.value.id] })
        if (res) {
          Message.success('撤回草稿箱成功')
          getPurchaseDetail()
        }
      },
    })
  }
</script>
<style lang="less" scoped>
  .purchase-detail-wrap {
    height: calc(100vh - @menuHeight);
    display: flex;
    flex-direction: column;
    .header {
      padding: 10px;
      background-color: #fff;
      margin-bottom: 10px;
    }
    .purchase-detail-con {
      flex: 1;
      background-color: #fff;
      margin-top: 10px;
      display: flex;
      flex-direction: column;
      overflow: hidden;
      .purchase-detail-order {
        flex: 1;
        display: flex;
        flex-direction: column;
        overflow: hidden;
        padding: 10px 20px;
      }
    }
    .base-w {
      background-color: #fff;
      padding: 20px 10px;
      .arco-steps {
        max-width: 1000px;
      }
      .label {
        display: inline-block;
        width: 100px;
        text-align: right;
      }
    }
  }
</style>
